package com.gm.englishdemo3.ui;

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.RadioButton;

import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;

import com.gm.englishdemo3.MyApplication;
import com.gm.englishdemo3.R;
import com.gm.englishdemo3.ui.home.HomeFragment;
import com.gm.englishdemo3.ui.user.UserFragment;

public class MainActivity extends AppCompatActivity {

    private final String TAG = "LIFE MainActivity";

    private Activity mActivityContext;

    private MyApplication myApplication;

    Fragment[] fragments = new Fragment[]{null, null};//存放Fragment

    //控件
    private LinearLayout llContent; //内容容器
    private RadioButton rbHome;//rb 主页
    private RadioButton rbUser;//rb 用户
    @Override
    protected void onCreate(Bundle savedInstanceState) {

        Log.d(TAG, "onCreate");

        super.onCreate(savedInstanceState);
        mActivityContext = this;
        myApplication = (MyApplication) getApplication();

        setContentView(R.layout.activity_main);

        //获取控件
        llContent = findViewById(R.id.ll_main_content);
        rbHome = findViewById(R.id.rb_main_home);
        rbUser = findViewById(R.id.rb_main_user);


        //页面初始化方法
        initView();

        //设置控件点击事件
        setViewEventListener();
    }

    private void setViewEventListener() {
        rbHome.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                switchFragment(0);
            }
        });
        rbUser.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                switchFragment(1);
            }
        });
    }

    private void switchFragment(int fragmentIndex) {
        //在Activity中显示Fragment
        //1、获取Fragment管理器 FragmentManager
        FragmentManager fragmentManager = this.getSupportFragmentManager();
        //2、开启fragment事务
        FragmentTransaction transaction = fragmentManager.beginTransaction();
        //懒加载 - 如果需要显示的Fragment为null，就new。并添加到Fragment事务中
        if (fragments[fragmentIndex] == null) {
            switch (fragmentIndex){
                case 0:
                    fragments[fragmentIndex]=new HomeFragment();
                    break;
                case 1:
                    fragments[fragmentIndex]=new UserFragment();
            }
            transaction.add(R.id.ll_main_content, fragments[fragmentIndex]);
        }
        for (int i = 0; i < fragments.length; i++) {
            if (fragmentIndex != i && fragments[i] != null) {
                //隐藏指定的Fragment
                transaction.hide(fragments[i]);
            }
        }
        transaction.show(fragments[fragmentIndex]);
        transaction.commit();
    }

    private void initView() {
        rbHome=findViewById(R.id.rb_main_home);
        rbUser=findViewById(R.id.rb_main_user);
    }
}
